package thread;

public class SynDemo2 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Shop shop2 = new Shop();
        Thread t1 = new Thread("王克兢") {
            public void run(){
                shop.buy();
                shop2.buy();
            }
        };
        Thread t2 = new Thread("gougou") {
            public void run(){
                shop.buy();
                shop2.buy();
            }

        };
        t1.start();
        t2.start();
    }

}
class Shop{
    public  void buy()  {
        Thread t1 = Thread.currentThread();

        try {
        System.out.println(t1.getName()+ "正在购买");

        synchronized (this) {
        System.out.println(t1.getName() + "正在试衣服");
        Thread.sleep(4000);
        }
        System.out.println(t1.getName() + "购买结束");}
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}